package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbRoom;
import com.ruoyi.system.domain.TbRoomDevice;
import com.ruoyi.system.mapper.TbRoomDeviceMapper;
import com.ruoyi.system.service.ITbRoomDeviceService;
import com.ruoyi.system.service.ITbRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房间管理Controller
 *
 * @author 朱欢乐
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/room/room")
public class TbRoomController extends BaseController {
    @Autowired
    private ITbRoomService tbRoomService;


    @Autowired
    ITbRoomDeviceService tbRoomDeviceService;

    @Autowired
    TbRoomDeviceMapper tbRoomDeviceMapper;

    /**
     * 查询房间管理列表
     */
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbRoom tbRoom) {
        startPage();
        List<TbRoom> list = tbRoomService.selectTbRoomList(tbRoom);
        for (TbRoom room : list) {
            TbRoomDevice tbRoomDevice = new TbRoomDevice();
            tbRoomDevice.setRoomId(room.getId());
            tbRoom.setRoomDeviceList(tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice));
        }
        return getDataTable(list);
    }

    /**
     * 导出房间管理列表
     */
    @PreAuthorize("@ss.hasPermi('room:room:export')")
    @Log(title = "房间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoom tbRoom) {
        List<TbRoom> list = tbRoomService.selectTbRoomList(tbRoom);
        ExcelUtil<TbRoom> util = new ExcelUtil<TbRoom>(TbRoom.class);
        util.exportExcel(response, list, "房间管理数据");
    }

    /**
     * 获取房间管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TbRoom tbRoom = tbRoomService.selectTbRoomById(id);
        TbRoomDevice tbRoomDevice = new TbRoomDevice();
        tbRoomDevice.setRoomId(id);
        tbRoom.setRoomDeviceList(tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice));
        return success(tbRoom);
    }

    /**
     * 新增房间管理
     */
    @PreAuthorize("@ss.hasPermi('room:room:add')")
    @Log(title = "房间管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoom tbRoom) {
        int i = tbRoomService.insertTbRoom(tbRoom);
        Long id = tbRoom.getId();
        List<TbRoomDevice> roomDeviceList = tbRoom.getRoomDeviceList();
        if (!CollectionUtils.isEmpty(roomDeviceList)) {
            for (TbRoomDevice tbRoomDevice : roomDeviceList) {
                tbRoomDevice.setRoomId(id);
                tbRoomDevice.setStId(Long.valueOf(tbRoom.getTmId()));
                tbRoomDevice.setState("0");
                tbRoomDeviceService.insertTbRoomDevice(tbRoomDevice);
            }
        }

        return toAjax(i);
    }

    /**
     * 修改房间管理
     */
    @PreAuthorize("@ss.hasPermi('room:room:edit')")
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRoom tbRoom) {
        int i = tbRoomService.updateTbRoom(tbRoom);
        tbRoomDeviceMapper.deleteTbRoomIDS(tbRoom.getId());
        Long id = tbRoom.getId();
        List<TbRoomDevice> roomDeviceList = tbRoom.getRoomDeviceList();
        if (!CollectionUtils.isEmpty(roomDeviceList)) {
            for (TbRoomDevice tbRoomDevice : roomDeviceList) {
                tbRoomDevice.setRoomId(id);
                tbRoomDevice.setStId(Long.valueOf(tbRoom.getTmId()));
                tbRoomDevice.setState("0");
                tbRoomDeviceService.insertTbRoomDevice(tbRoomDevice);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除房间管理
     */
    @PreAuthorize("@ss.hasPermi('room:room:remove')")
    @Log(title = "房间管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            tbRoomDeviceMapper.deleteTbRoomIDS(id);
        }
        return toAjax(tbRoomService.deleteTbRoomByIds(ids));
    }
}
